<?php
namespace Home\Controller;
use Think\Controller;
class ApplyController extends Controller{
	public function _initialize(){
		if($_COOKIE["manager_id"] == null){
			$index = new IndexController();
			$index->login();exit;
		}
	}
	
	public function index(){
		
	}
	public function showApply(){
		$this->display();
	}
	public function getApply(){
		$apply=M("Applycash");
		$sql="select a.*,e.realname from applycash as a left join erector as e on a.erector_id = e.user_id where apply_statue ='apply'";
		$allapply = $apply->query($sql);
		for($i=0;$i<count($allapply);$i++){
			$allapply[$i]["apply_time"] = date("Y年m月d日 H时i分",$allapply[$i]["apply_time"]);
		
			$allapply[$i]["contro"] = "<a href='/Home/Apply/doneApply?id=".$allapply[$i]['id']."'>处理</a>";
		}
		
		echo json_encode($allapply);
	}
	public function doneApply(){
		$apply=M("Applycash");
		$apply->id = $_GET["id"];
		$apply->payer = $_COOKIE["manager_id"];
		$apply->pay_time = time();
		$apply->apply_statue = "success";
		if($apply->save()){
			$this->success("处理完成",showApply,2);
		}else{
			$this->error("处理失败",showApply,2);
		}
	}
	public function showAll(){
		$this->display();
	}
	public function getAll(){
		$apply=M("Applycash");
		$sql="select a.*,e.realname from applycash as a left join erector as e on a.erector_id = e.user_id where apply_statue ='success'";
		$allapply = $apply->query($sql);
		for($i=0;$i<count($allapply);$i++){
			$allapply[$i]["apply_time"] = date("Y年m月d日 H时i分",$allapply[$i]["apply_time"]);
			$allapply[$i]["pay_time"] = date("Y年m月d日 H时i分",$allapply[$i]["pay_time"]);
			$id = $allapply[$i]["payer"];
			$allapply[$i]["payer"] = M("Manage")->where("id = $id")->field("managename")->find()["managename"];
		}
		
		echo json_encode($allapply);
	}
}